package com.pason.onlineshop.mainshop.functionaltest.framework;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Selenium Grid server Remote desktop Host : webtest01.ae.sda.corp.telstra.com User/Pass:
 * admin/Password1
 */
public class WebDriverFactory {
    public static final String HTMLUNIT = "HTMLUNIT";
    public static final String IE = "IE";
    public static final String FIREFOX = "FIREFOX";
    public static final String CHROME = "CHROME";
    public static final String REMOTE = "REMOTE";
    // webhub01 selenium hub has three nodes supporting 3 browsers, IE, Chrome and Firefox 
    //public static final String REMOTE_HOST = "http://webhub01.ae.sda.corp.telstra.com:4444/wd/hub";
    // public static final String REMOTE_HOST = "http://webtest02.ae.sda.corp.telstra.com:4444/wd/hub";
    public static final String REMOTE_HOST = "http://webtest01.ae.sda.corp.telstra.com:4444/wd/hub";
     //public static final String REMOTE_HOST = "http://a00000117004300.wg.dir.telstra.com:4444/wd/hub";

    protected static final Logger logger = LoggerFactory.getLogger(WebDriverFactory.class);

    public static WebDriver createWebDriver(final String webDriverType) {
        System.out.println("WebDriver type: " + webDriverType);

        WebDriver result = null;
        if (HTMLUNIT.equalsIgnoreCase(webDriverType)) {
            result = new ExtendedHtmlUnitDriver();
        } else if (IE.equalsIgnoreCase(webDriverType)) {
            DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
            ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            result = new InternetExplorerDriver(ieCapabilities);
        } else if (CHROME.equalsIgnoreCase(webDriverType)) {
            result = new ChromeDriver();
        } else if (REMOTE.equalsIgnoreCase(webDriverType)) {
            result = remoteChromeDriver();
        } else if (FIREFOX.equalsIgnoreCase(webDriverType)) {
            result = fireFoxDriver();
        } else {
            logger.debug("Invalid browser type '" + webDriverType + "': defaulting to Firefox.");
            result = fireFoxDriver();
        }

        result.manage().timeouts().setScriptTimeout(5000, TimeUnit.MILLISECONDS);
        return result;
    }

    private static WebDriver fireFoxDriver() {
        Proxy proxy = new Proxy();
        proxy.setProxyAutoconfigUrl("http://wpad.in.telstra.com.au/proxy.pac");
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.PROXY, proxy);
        return new FirefoxDriver(cap);
    }

    private static WebDriver remoteChromeDriver() {
        WebDriver result;
        try {
            result = new RemoteWebDriver(new URL(REMOTE_HOST), DesiredCapabilities.chrome());
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

}

